package com.songshuai.webapi.baseapi.config;

import com.songshuai.webapi.baseapi.util.RedisUtil;
import com.songshuai.webapi.baseapi.util.SnowFlakeUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by yubo on 12/11/2018.
 */
@Configuration
public class BeanConfig {

    @Resource
    RedisTemplate redisTemplate;

    @Bean
    public SnowFlakeUtil snowFlakeUtil() {
        //:todo eureka dispatch id
        return new SnowFlakeUtil(1, 1);
    }

    @Bean
    public RedisUtil redisUtil() {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        return redisUtil;
    }

    @Bean
    public ConcurrentTaskScheduler concurrentTaskScheduler() {
        ConcurrentTaskScheduler scheduler = new ConcurrentTaskScheduler();
        return scheduler;
    }



    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        RestTemplate restTemplete=new RestTemplate(factory);
        return  restTemplete;
    }


    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(5000);//毫秒
        factory.setReadTimeout(5000);
        return factory;
    }
}
